function [labels] = extractLabels(fileName,n)
%Given a MNIST label file, extracts the first n labels.

%Open file for reading in big-endian format
dataFile = fopen(fileName,'r','b');
assert(dataFile ~= -1,'Problem opening MNIST label file');

%Check magic number
magic=fread(dataFile,1,'int32');
assert(magic == 2049,'Invalid magic number, not a MNIST label file')

numLabels=fread(dataFile,1,'int32');
n=min(n,numLabels);

labels=(fread(dataFile,n,'uchar'))';

fclose(dataFile);

end

